/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*!
  @file LayerItem.h
  @author Darryl Wallace
  @date 2011
  @brief This file defines the layer item.  A layer item is a QGraphicsItem that holds the image for drawing.
  */

#ifndef LAYERITEM_H
#define LAYERITEM_H

#include <QtCore>
#include <QtGui>
#include "LibKanvas_global.h"
#include "Canvas.h"

class ITool;


/// The layer object
/*!
  This class is the layer item that gets operated on by mouse events.
  */
class LIBKANVAS_EXPORT LayerItem : public QGraphicsItem
{
public:
    /// Constructor
    /*!
      \param parent This parent is the main background item of the graphics scene.
      */
    LayerItem(QGraphicsRectItem *parent=0);

    /// Destructor
    ~LayerItem() {};

    /// Gets the rect of the layer
    /*!
      \return bounding rect of layer
      */
    QRectF boundingRect() const;

    /// Gets the layer's image.
    /*!
      This is primarily used for writing the layer images to file.
      \return layer image.
      */
    const QImage & getImage() const;

    /// Gets a pointer to the layer image
    /*!
      Certain tools may require to directly operate on the image's layer,
      e.g. Eraser tools
      \return pointer to the layer image.
      */
    QImage *getImagePtr();


    /// Sets the layer's image
    /*!
      This method is required for setting the layer image when loading an image from disk.
      \param image The image that is to be loaded.
      */
    void setImage(const QImage &image);

    /// Resize the layer
    /*!
      When resizing the canvas, each layer must be resized as well.
      The anchor location indicates the position that the original
      image will be resized around.

      For example, resizing the image with the center anchor location would add the difference
      from each side.
      \param width The new width of the layer.
      \param height The new height of the layer.
      \param anchorLocation Resize anchor location.
      */
    void resize(int width, int height, Canvas::ResizeAnchor anchorLocation=Canvas::TopLeft);

    /// Scale the image
    /*!
      When scaling the entire canvas, each layer must be scaled as well.
      \param sx scale value in the x-axis as a multiplication factor.
      \param sy scale value in the y-axis as a multiplication factor.
      */
    void scaleImage(qreal sx, qreal sy);

    /// Fill the entire image with a starting color
    /*!
      The first layer of a new project will have white, each subsequent layer will be
      translucent.
      \param color The colour that the image will be filled with.
      */
    void fillImage(const QColor &color);

    /// Reimplemented from QGraphicsItem.
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0);

    /// Adds and paints an the item onto the layer image.
    /*!
      When a tool is finished drawing (typically on mouse release), the canvas will take the graphics item that is
      the result of the tool and add it to the layer.
      \param item The ITool to be drawn on the canvas.
      */
    void addItem(ITool *item);

private:
    QImage _layerImage;
    QGraphicsRectItem *_parent;
};

#endif //LAYERITEM_H
